package com.tfswx.fljdmxyy.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.newbie.core.exception.BusinessException;
import com.tfswx.fljdmxyy.constant.SystemConstant;
import com.tfswx.fljdmxyy.dao.DmXtFldmMapper;
import com.tfswx.fljdmxyy.dao.DmXtFldmlbMapper;
import com.tfswx.fljdmxyy.dto.sjzd.*;
import com.tfswx.fljdmxyy.enums.BusinessCodeEnum;
import com.tfswx.fljdmxyy.model.DmXtFldm;
import com.tfswx.fljdmxyy.model.DmXtFldmlb;
import com.tfswx.fljdmxyy.query.sjzd.FldmQueryParam;
import com.tfswx.fljdmxyy.query.sjzd.FldmQueryResult;
import com.tfswx.fljdmxyy.query.sjzd.FldmlbQueryParam;
import com.tfswx.fljdmxyy.query.sjzd.FldmlbQueryResult;
import com.tfswx.fljdmxyy.service.SjzdService;
import com.tfswx.fljdmxyy.utils.UuidUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 数据字典实现类
 *
 * @Author 陈文佳
 * @Date 2023/03/24
 */
@Service
public class SjzdServiceImpl implements SjzdService {

    @Resource
    private DmXtFldmlbMapper fldmlbMapper;

    @Resource
    private DmXtFldmMapper fldmMapper;

    @Override
    public FldmGetOutputDTO getFldm(FldmGetInputDTO dto) {
        DmXtFldm fldm = Convert.convert(DmXtFldm.class, dto);
        return Convert.convert(FldmGetOutputDTO.class, fldmMapper.getDmXtFldm(fldm));
    }

    @Override
    public FldmlbAddOutputDTO addFldmlb(FldmlbAddInputDTO dto) {
        DmXtFldmlb fldmlb = Convert.convert(DmXtFldmlb.class, dto);
        // 1.查询该类别编码是否已存在
        if (ObjectUtils.isNotEmpty(fldmlbMapper.getFldmlb(fldmlb))) {
            throw new BusinessException(BusinessCodeEnum.TJSJSB);
        }

        // 2.新增分类代码类别
        Date now = new Date();
        fldmlb.setLbbm(UuidUtils.uuid32());
        fldmlb.setCjsj(now);
        fldmlb.setZhxgsj(now);
        fldmlb.setSfsc(SystemConstant.N);
        if (StringUtils.isEmpty(dto.getLbxh())) {
            // 3.设置分类代码类别序号
            Integer maxXh = fldmlbMapper.getMaxXh();
            if (ObjectUtil.isNull(maxXh)) {
                maxXh = 0;
            }
            fldmlb.setLbxh(Long.valueOf(maxXh) + 1);
        }
        int res = fldmlbMapper.insertDmXtFldmlb(fldmlb);
        if (res < 1) {
            throw new BusinessException(BusinessCodeEnum.TJSJSB);
        }
        return Convert.convert(FldmlbAddOutputDTO.class, fldmlb);
    }

    @Override
    public List<FldmlbQueryResult> queryFldmlb(FldmlbQueryParam param) {
        return fldmlbMapper.queryDmXtFldmlb(param);
    }

    @Override
    public Boolean updateFldmlb(FldmlbUpdateInputDTO dto) {
        DmXtFldmlb fldmlb = Convert.convert(DmXtFldmlb.class, dto);
        fldmlb.setZhxgsj(new Date());
        int res = fldmlbMapper.updateDmXtFldmlb(fldmlb);
        if (res < 1) {
            throw new BusinessException(BusinessCodeEnum.SCSJSB);
        }
        return Boolean.TRUE;
    }

    @Override
    public FldmAddOutputDTO addFldm(FldmAddInputDTO dto) {
        // 1.设置分类代码属性
        DmXtFldm fldm = Convert.convert(DmXtFldm.class, dto);
        Date now = new Date();
        fldm.setDm(UuidUtils.uuid32());
        fldm.setCjsj(now);
        fldm.setZhxgsj(now);
        fldm.setSfsc(SystemConstant.N);
        if (StringUtils.isEmpty(dto.getDmxh())) {
            // 2.设置分类代码序号
            Integer maxXh = fldmMapper.getMaxXh(dto.getLbbm());
            if (ObjectUtil.isNull(maxXh)) {
                maxXh = 0;
            }
            fldm.setDmxh(Long.valueOf(maxXh) + 1);
        }

        // 3.新增分类代码
        int res = fldmMapper.insertDmXtFldm(fldm);
        if (res < 1) {
            throw new BusinessException(BusinessCodeEnum.TJSJSB);
        }
        return Convert.convert(FldmAddOutputDTO.class, fldm);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteFldmlb(FldmlbDeleteInputDTO dto) {
        dto.getLbbm().forEach(lbbm -> {
            // 1.删除分类代码类别
            DmXtFldmlb fldmlb = new DmXtFldmlb();
            fldmlb.setLbbm(lbbm);
            fldmlb.setZhxgsj(new Date());
            int res = fldmlbMapper.deleteDmXtFldmlb(fldmlb);
            if (res < 1) {
                throw new BusinessException(BusinessCodeEnum.SCSB);
            }

            // 2.删除该类别下的分类代码（由于该类别下可能不存在分类代码，所以不对修改条数进行检查）
            DmXtFldm dm = new DmXtFldm();
            dm.setZhxgsj(new Date());
            dm.setLbbm(lbbm);
            fldmMapper.deleteByLbbm(dm);
        });
        return Boolean.TRUE;
    }

    @Override
    public List<FldmQueryResult> queryFldm(FldmQueryParam param) {
        return fldmMapper.queryDmXtFldm(param);
    }

    @Override
    public Boolean updateFldm(FldmUpdateInputDTO dto) {
        DmXtFldm fldm = Convert.convert(DmXtFldm.class, dto);
        // 1.父代码不能是自己
        if (StringUtils.equals(dto.getDm(), dto.getFdm())) {
            throw new BusinessException(BusinessCodeEnum.XGSJSB);
        }

        // 2.修改分类代码
        fldm.setZhxgsj(new Date());
        int res = fldmMapper.updateDmXtFldm(fldm);
        if (res < 1) {
            throw new BusinessException(BusinessCodeEnum.XGSJSB);
        }
        return Boolean.TRUE;
    }

    @Override
    public FldmlbGetOutputDTO getFldmlb(FldmlbGetInputDTO dto) {
        DmXtFldmlb fldmlb = Convert.convert(DmXtFldmlb.class, dto);
        return Convert.convert(FldmlbGetOutputDTO.class, fldmlbMapper.getDmXtFldmlb(fldmlb));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteFldm(FldmDeleteInputDTO dto) {
        dto.getDm().forEach(dm -> {
            DmXtFldm fldm = new DmXtFldm();
            fldm.setDm(dm);
            fldm.setZhxgsj(new Date());
            int res = fldmMapper.deleteDmXtFldm(fldm);
            if (res < 1) {
                throw new BusinessException(BusinessCodeEnum.SCSB);
            }
        });
        return Boolean.TRUE;
    }
}
